package com.woniuxy.xiangruicommonutilserver.controller;

/**
 * Auther: wuson  <br/>
 * Date: 2021/4/14:20:39  <br/>
 * Description:
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.woniuxy.bean.ResultBean;
import com.woniuxy.bean.CertificationEntity;
import com.woniuxy.xiangruicommonutilserver.dao.AppCodeDao;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@RefreshScope
@Api(tags = "身份实名相关接口")
@RequestMapping("/certification")
@RestController
public class CertificationController {
    //    @Resource
//    private CertificationService certificationService;
//    @Value("${xiangrui.ali.appcode}")
//    private String appcode;
    @Resource
    private AppCodeDao appCodeDao;

    @ApiOperation("实名认证")
    @PostMapping("/send")
    public ResultBean sendMessage (@RequestParam("idcardName") String idcardName, @RequestParam("idcard") String idcard) {
        String url = "https://eid.shumaidata.com/eid/check";
        String appCode = appCodeDao.getAppCode("certification");//appcode
        Map<String, String> params = new HashMap<>();
        params.put("idcard", idcard);
        params.put("name", idcardName);
        String result = null;
        try {
            result = postForm(appCode, url, params);
        } catch (IOException e) {
            e.printStackTrace();
        }
        CertificationEntity certificationEntity = certificationEntity = JSON.parseObject(JSON.parseObject(result).getString("result"), CertificationEntity.class);
        return ResultBean.builder().code(200).msg("实名认证成功").data(certificationEntity).build();
    }

    /**
     * 用到的HTTP工具包：okhttp 3.13.1
     * <dependency>
     * <groupId>com.squareup.okhttp3</groupId>
     * <artifactId>okhttp</artifactId>
     * <version>3.13.1</version>
     * </dependency>
     */
    public static String postForm (String appCode, String url, Map<String, String> params) throws IOException {
        OkHttpClient client = new OkHttpClient.Builder().build();
        FormBody.Builder formbuilder = new FormBody.Builder();
        Iterator<String> it = params.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            formbuilder.add(key, params.get(key));
        }
        FormBody body = formbuilder.build();
        Request request = new Request.Builder().url(url).addHeader("Authorization", "APPCODE " + appCode).post(body).build();
        Response response = client.newCall(request).execute();
        String result = response.body().string();
        return result;
    }
}